package com.jservice.web.index;

import com.jfinal.plugin.activerecord.Db;
import com.jservice.framework.annotation.RouteBind;
import com.jservice.framework.utils.DateUtils;
import com.jservice.model.SysUser;
import com.jservice.web.base.JBaseController;

/**
 * IndexController
 *
 * @Author cwledit
 * @DATE 2017/4/26.
 */
@RouteBind(path = "/admin")
public class IndexController extends JBaseController{
    /**
     * 主页
     */
    public void index(){
        //获得当前用户
        SysUser cUser = getCUser();
        setAttr("cUser",cUser);
        render("/admin/index/index") ;
    }

    /**
     * 首页
     */
    public void main(){
        String sqlRechargeAmount = "SELECT sum(amount) FROM `smk_recharge` where `status`IN ('3','5','7','8') and create_date >= ? and create_date <= ? ";
        String sqlRechargeAmountAll = "SELECT sum(amount) FROM `smk_recharge` where `status`IN ('3','5','7','8')";
        Double recharge_day_amount = Db.queryDouble(sqlRechargeAmount,DateUtils.getDayBegin(),DateUtils.getDayEnd());
        Double recharge_month_amount = Db.queryDouble(sqlRechargeAmount,DateUtils.getBeginDayOfMonth(),DateUtils.getEndDayOfMonth());
        Double recharge_year_amount = Db.queryDouble(sqlRechargeAmount,DateUtils.getBeginDayOfYear(),DateUtils.getEndDayOfYear());
        Double recharge_all_amount = Db.queryDouble(sqlRechargeAmountAll);
        if (recharge_day_amount == null){
            recharge_day_amount = new Double(0);
        }
        if (recharge_month_amount == null){
            recharge_month_amount = new Double(0);
        }
        if (recharge_year_amount == null){
            recharge_year_amount = new Double(0);
        }
        if (recharge_all_amount == null){
            recharge_all_amount = new Double(0);
        }
        setAttr("recharge_day_amount",recharge_day_amount);
        setAttr("recharge_month_amount",recharge_month_amount);
        setAttr("recharge_year_amount",recharge_year_amount);
        setAttr("recharge_all_amount",recharge_all_amount);

        String sqlRechargeCount = "SELECT count(*) FROM `smk_recharge` where `status`IN ('3','5','7','8') and create_date >= ? and create_date <= ? ";
        String sqlRechargeCountAll = "SELECT count(*) FROM `smk_recharge` where `status`IN ('3','5','7','8') ";
        Long recharge_day_count = Db.queryLong(sqlRechargeCount,DateUtils.getDayBegin(),DateUtils.getDayEnd());
        Long recharge_month_count = Db.queryLong(sqlRechargeCount,DateUtils.getBeginDayOfMonth(),DateUtils.getEndDayOfMonth());
        Long recharge_year_count = Db.queryLong(sqlRechargeCount,DateUtils.getBeginDayOfYear(),DateUtils.getEndDayOfYear());
        Long recharge_all_count = Db.queryLong(sqlRechargeCountAll);
        setAttr("recharge_day_count",recharge_day_count);
        setAttr("recharge_month_count",recharge_month_count);
        setAttr("recharge_year_count",recharge_year_count);
        setAttr("recharge_all_count",recharge_all_count);

        String sqlRefundsAmount = "SELECT sum(amount) FROM `smk_refunds` where `status`IN ('3','4') and create_date >= ? and create_date <= ? ";
        String sqlRefundsAmountAll = "SELECT sum(amount) FROM `smk_refunds` where `status`IN ('3','4')";
        Double refunds_day_amount = Db.queryDouble(sqlRefundsAmount,DateUtils.getDayBegin(),DateUtils.getDayEnd());
        Double refunds_month_amount = Db.queryDouble(sqlRefundsAmount,DateUtils.getBeginDayOfMonth(),DateUtils.getEndDayOfMonth());
        Double refunds_year_amount = Db.queryDouble(sqlRefundsAmount,DateUtils.getBeginDayOfYear(),DateUtils.getEndDayOfYear());
        Double refunds_all_amount = Db.queryDouble(sqlRefundsAmountAll);
        if (refunds_day_amount == null){
            refunds_day_amount = new Double(0);
        }
        if (refunds_month_amount == null){
            refunds_month_amount = new Double(0);
        }
        if (refunds_year_amount == null){
            refunds_year_amount = new Double(0);
        }
        if (refunds_all_amount == null){
            refunds_all_amount = new Double(0);
        }
        setAttr("refunds_day_amount",refunds_day_amount);
        setAttr("refunds_month_amount",refunds_month_amount);
        setAttr("refunds_year_amount",refunds_year_amount);
        setAttr("refunds_all_amount",refunds_all_amount);

        String sqlRefundsCount = "SELECT count(*) FROM `smk_refunds` where `status`IN ('3','4') and create_date >= ? and create_date <= ? ";
        String sqlRefundsCountAll = "SELECT count(*) FROM `smk_refunds` where `status`IN ('3','4') ";
        Long refunds_day_count = Db.queryLong(sqlRefundsCount,DateUtils.getDayBegin(),DateUtils.getDayEnd());
        Long refunds_month_count = Db.queryLong(sqlRefundsCount,DateUtils.getBeginDayOfMonth(),DateUtils.getEndDayOfMonth());
        Long refunds_year_count = Db.queryLong(sqlRefundsCount,DateUtils.getBeginDayOfYear(),DateUtils.getEndDayOfYear());
        Long refunds_all_count = Db.queryLong(sqlRefundsCountAll);
        setAttr("refunds_day_count",refunds_day_count);
        setAttr("refunds_month_count",refunds_month_count);
        setAttr("refunds_year_count",refunds_year_count);
        setAttr("refunds_all_count",refunds_all_count);

        String sqlAppUserCount = "SELECT count(*) FROM `smk_app_user` where login_type = ?";
        String sqlAppUserCountAll = "SELECT count(*) FROM `smk_app_user`";
        Long zww_count = Db.queryLong(sqlAppUserCount,"zww");
        Long smk_count = Db.queryLong(sqlAppUserCount,"smk");
        Long all_count = Db.queryLong(sqlAppUserCountAll);
        setAttr("zww_count",zww_count);
        setAttr("smk_count",smk_count);
        setAttr("all_count",all_count);

        render("/admin/index/main");
    }

}
